package com.zqwh.storemonitor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.videogo.openapi.EzvizAPI;
import com.zqwh.storemonitor.R;
import com.zqwh.storemonitor.domain.ShopWatcher;
import com.zqwh.storemonitor.model.WatcherModel;
import com.zqwh.storemonitor.service.NotificationService;
import com.zqwh.yj.sdk.utils.SharePreferencesUtils;
import com.zqwh.yj.sdk.utils.StringUtils;
import com.zqwh.yj.sdk.utils.ToastUtils;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;

import static com.zqwh.storemonitor.StoreMonitorApplication.getOpenSDK;
import static com.zqwh.storemonitor.StoreMonitorApplication.sGeTuiCid;
import static com.zqwh.storemonitor.StoreMonitorApplication.sShopWatcher;


public class LoginActivity extends BaseActivity {
    private EditText etAccount;
    private EditText etPwd;
    private Button btnLogin;

    private WatcherModel watcherModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        watcherModel = new WatcherModel(this);

        setView();
        setClickListener();
    }

    private void setView() {
        etAccount = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        btnLogin = findViewById(R.id.btn_login);
    }

    private void setClickListener(){
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String account = etAccount.getText().toString();
                if (StringUtils.isEmpty(account)){
                    ToastUtils.showLongToast(LoginActivity.this, "账号不能为空");
                    return;
                }
                final String pwd = etPwd.getText().toString();
                if (StringUtils.isEmpty(pwd)){
                    ToastUtils.showLongToast(LoginActivity.this, "密码不能为空");
                    return;
                }
                watcherModel.login(account, pwd, new NetCallback<ShopWatcher>() {
                    @Override
                    public void onSuccess(final ShopWatcher shopWatcher) {
                        watcherModel.setGeTuiCid(shopWatcher.getShopWatcherId(), sGeTuiCid, new NetCallback<Object>() {
                            @Override
                            public void onSuccess(Object o) {
                                SharePreferencesUtils.writeStringSharedPreferences(LoginActivity.this,"ACCOUNT", account);
                                SharePreferencesUtils.writeStringSharedPreferences(LoginActivity.this,"PWD", pwd);
                                sShopWatcher = shopWatcher;
                                startService(new Intent(LoginActivity.this, NotificationService.class));
                                jumpToStoreListActivity();
                            }

                            @Override
                            public void onError(String msg) {

                            }
                        });
                    }

                    @Override
                    public void onError(String msg) {
                        ToastUtils.showLongToast(LoginActivity.this, msg);
                    }
                });
            }
        });
    }

    private void jumpToStoreListActivity() {
        if (EzvizAPI.getInstance().isLogin()) {
            startActivity(new Intent(this, StoreListActivity.class));
            finish();
        }else{
            getOpenSDK().openLoginPage();
        }
    }
}
